package com.jinlejia.erp.ui.message;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.jinlejia.erp.R;
import com.jinlejia.erp.databinding.FragmentMessageBinding;
import com.sevenheaven.segmentcontrol.SegmentControl;

/**
 * 消息（Fragment）
 */

public class MessageFragment extends Fragment {

    private View view;
    private int segmentIndex = 0;

    private FragmentMessageBinding binding;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        if (view == null) {
            view = inflater.inflate(R.layout.fragment_message, null);
            binding = DataBindingUtil.bind(view);
            initView();
        }
        // 缓存的rootView需要判断是否已经被加过parent，
        // 如果有parent需要从parent删除，要不然会发生这个rootview已经有parent的错误。
        ViewGroup parent = (ViewGroup) view.getParent();
        if (parent != null) {
            parent.removeView(view);
        }
        FragmentManager fm = getChildFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        if (segmentIndex == 0)
            ft.replace(R.id.content, new MessageListFragment());
        else if (segmentIndex == 1)
            //TODO 聊天
            ft.replace(R.id.content, new MessageListFragment());
        ft.commitAllowingStateLoss();

        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    private void initView() {
        binding.segmentControl.setOnSegmentControlClickListener(new SegmentControl.OnSegmentControlClickListener() {
            @Override
            public void onSegmentControlClick(int index) {
                hideSoftKeyboard();
                segmentIndex = index;
                FragmentManager fm = getChildFragmentManager();
                FragmentTransaction ft = fm.beginTransaction();
                switch (index) {
                    case 0:
                        ft.replace(R.id.content, new MessageListFragment());
                        ft.commitAllowingStateLoss();
                        break;
                    case 1:
                        ft.replace(R.id.content, new MessageListFragment());
                        ft.commitAllowingStateLoss();
                        break;
                    default:
                        break;
                }
            }
        });
    }

    protected void hideSoftKeyboard() {
        InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) {
            imm.hideSoftInputFromWindow(getActivity().getWindow().getDecorView().getWindowToken(),
                    0);
        }
    }

}
